package com.gxuwz.medical.domain.payment;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.tools.DateUtil;

/**
 * 缴费模块类
 * 
 * @author 演示
 * 
 */
public class Payment {
	/**
	 * 缴费编号
	 */
	private String payNo;
	/**
	 * 农合证号
	 */
	private String perscode;
	/**
	 * 缴费金额
	 */
	private double payCost;
	/**
	 * 执行年度
	 */
	private int runyear;
	/**
	 * 缴费时间
	 */
	private java.util.Date paytime;
	/**
	 * 经办人
	 */
	private String creator;

	public Payment() {

	}

	/**
	 * 参合缴费
	 * 
	 * @param perscodes
	 *            -要缴费家庭成员农合证号集合
	 * @param payperiod
	 *            -缴费标准
	 * @param creator
	 *            -操作员
	 * @throws Exception
	 */
	public void addPay(String[] perscodes, Payperiod payperiod, String creator)
			throws Exception {

		Connection conn = DbUtil.getConn();
		try {
			// 1:生成缴费单号
			this.payNo = "P"
					+ DateUtil.format(new java.util.Date(),
							DateUtil.DFMT_YYYYMMDDHHMMSS);
			// 缴费时间
			this.paytime = new java.util.Date();
			// 缴费标准信息
			this.payCost = payperiod.getAmount();
			// 缴费年份
			this.runyear = payperiod.getRunyear();
			// 经办人
			this.creator = creator;
			// 手动开启事务
			conn.setAutoCommit(false);
			// 持久化到数据库
			saveToDB(conn, perscodes);
			conn.commit();

		} catch (Exception e) {
			e.printStackTrace();
			if (conn != null) {
				conn.rollback();
			}
			throw new Exception("参合缴费登记失败!", e);
		} finally {
			DbUtil.close(conn);
		}
	}

	/**
	 * Batch方式持久化数据
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn, String[] perscodes)
			throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer("insert into t_payment (");
			sqlBuff.append("payNo,perscode,payCost,runyear,paytime,creator)");
			sqlBuff.append("values(?,?,?,?,?,?)");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			for (String perscode : perscodes) {
				this.perscode = perscode;
				int index = 1;
				pstmt.setString(index++, this.payNo);
				pstmt.setString(index++, this.perscode);
				pstmt.setDouble(index++, this.payCost);
				pstmt.setInt(index++, this.runyear);
				pstmt.setDate(index++,
						new java.sql.Date(this.paytime.getTime()));
				pstmt.setString(index++, this.creator);
				pstmt.addBatch();
			}
			int counts[] = pstmt.executeBatch();

		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}

	public double getPayCost() {
		return payCost;
	}

	public void setPayCost(double payCost) {
		this.payCost = payCost;
	}

	public int getRunyear() {
		return runyear;
	}

	public void setRunyear(int runyear) {
		this.runyear = runyear;
	}

	public java.util.Date getPaytime() {
		return paytime;
	}

	public void setPaytime(java.util.Date paytime) {
		this.paytime = paytime;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public String getPayNo() {
		return payNo;
	}

	public void setPayNo(String payNo) {
		this.payNo = payNo;
	}

}
